{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-07T12:42:11.988189Z",
     "start_time": "2025-09-07T12:42:11.984529Z"
    }
   },
   "source": "from struct import Struct",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:43:20.752488Z",
     "start_time": "2025-09-07T12:43:20.745730Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_struct = Struct('i?f')  # int bool float\n",
    "data = my_struct.pack(42, False, 15.8)\n",
    "data"
   ],
   "id": "403de82e91c009f8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'*\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xcd\\xcc|A'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:43:51.332928Z",
     "start_time": "2025-09-07T12:43:51.329666Z"
    }
   },
   "cell_type": "code",
   "source": [
    "unpack_data = my_struct.unpack(data)\n",
    "unpack_data"
   ],
   "id": "65b0b4d6189c118c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, False, 15.800000190734863)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:44:36.631809Z",
     "start_time": "2025-09-07T12:44:36.628181Z"
    }
   },
   "cell_type": "code",
   "source": "from collections import namedtuple",
   "id": "4eb5a6ef681ef0eb",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:45:45.174564Z",
     "start_time": "2025-09-07T12:45:45.171689Z"
    }
   },
   "cell_type": "code",
   "source": "Person = namedtuple('Person', ['name', 'desc', 'age', 'upgraded', 'score'])",
   "id": "7e605352f49504f3",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:48:06.456037Z",
     "start_time": "2025-09-07T12:48:06.450026Z"
    }
   },
   "cell_type": "code",
   "source": [
    "person = Person(\n",
    "    bytes('Bobby', encoding='utf-8'),\n",
    "    bytes('Hello, Bobby测试', encoding='utf-8'),\n",
    "    28,\n",
    "    True,\n",
    "    200.0\n",
    ")\n",
    "person"
   ],
   "id": "5b40213cffa982d5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Person(name=b'Bobby', desc=b'Hello, Bobby\\xe6\\xb5\\x8b\\xe8\\xaf\\x95', age=28, upgraded=True, score=200.0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:49:09.070085Z",
     "start_time": "2025-09-07T12:49:09.066458Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_struct = Struct(f'{len(person.name)}s{len(person.desc)}si?f')\n",
    "data = my_struct.pack(*person)\n",
    "data"
   ],
   "id": "83562d18e6a995f1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'BobbyHello, Bobby\\xe6\\xb5\\x8b\\xe8\\xaf\\x95\\x00\\x1c\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00HC'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:49:47.346588Z",
     "start_time": "2025-09-07T12:49:47.342879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "unpack_data = my_struct.unpack(data)\n",
    "person = Person._make(unpack_data)\n",
    "person"
   ],
   "id": "41bc3ca392c31959",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Person(name=b'Bobby', desc=b'Hello, Bobby\\xe6\\xb5\\x8b\\xe8\\xaf\\x95', age=28, upgraded=True, score=200.0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T12:50:11.841589Z",
     "start_time": "2025-09-07T12:50:11.837653Z"
    }
   },
   "cell_type": "code",
   "source": "unpack_data",
   "id": "935d5d47022da10c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(b'Bobby', b'Hello, Bobby\\xe6\\xb5\\x8b\\xe8\\xaf\\x95', 28, True, 200.0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
